<!-- 站点编辑弹窗 -->
<template>
  <el-dialog :destroy-on-close="true" :lock-scroll="false" :title="isUpdate?'修改分类':'添加分类'" :visible="visible" width="750px" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="分类名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入品牌名称" />
      </el-form-item>
      <el-form-item label="父级分类：">
        <TreeSelect :noChildrenText="'没有子项'" :options="categoryList" v-model="form.parent_id" :defaultExpandLevel="0" :normalizer="normalizer" placeholder="请选择产品分类" />
      </el-form-item>
      <el-form-item label="排序：" prop="sort_order">
        <el-input v-model="form.sort_order" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="状态：">
        <el-switch v-model="form.status" :active-value=1 :inactive-value=0 active-color="#3979FC" active-text="启用" inactive-color="rgba(0, 0, 0, 0.2)" inactive-text="禁止" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import TreeSelect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'CategoryEdit',
  components: { TreeSelect },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 }, this.data),
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      categoryList: [],
      rules: {
        name: [
          { required: true, message: "请输入品牌名称", trigger: "blur" }
        ],
        sort_order: [
          { required: true, message: "请输入排序", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign(this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  created() {
    this.getCategory();
  },
  methods: {
    getCategory() {
      this.$http.get("/categories/tree").then(res => {
        this.categoryList = res.data.data
      });
    },
    // 下拉树格式化
    normalizer(d) {
      return {
        id: d.id,
        label: d.name,
        children: d.children || undefined
      }
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          // 编辑
          if (this.isUpdate) {
            this.$emit("editCategories", this.form, this.form.id);
          } else {
            // 新增
            this.$emit("addCategories", this.form);
            this.$nextTick(() => {
              this.form = {};
              this.category_name = "";
            })
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.updateVisible(false);
      this.category_name = "";
      this.form = {};
      this.isUpdate = false;
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>


<style lang="scss">
</style>
